package com.clothes.views;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.clothes.R;
import com.gyf.barlibrary.ImmersionBar;

/**
 * Created by 晓旭 on 2016/4/22.
 */
public class SearchDialog {
    private Activity context;
    private Dialog dialog;
    private Display display;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;

    public SearchDialog(Activity context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        builder();
    }

    public SearchDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(R.layout.view_actionsheet, null);
        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());
        // 获取自定义Dialog布局中的控件
        tv1 =  view.findViewById(R.id.tv1);
        tv2 =  view.findViewById(R.id.tv2);
        tv3 = view.findViewById(R.id.tv3);
        tv4 =  view.findViewById(R.id.tv4);
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(goListener != null){
                    goListener.one();
                }
                dismiss();
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(goListener != null){
                    goListener.two();
                }
                dismiss();
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(goListener != null){
                    goListener.three();
                }
                dismiss();
            }
        });

        tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.Translucent_NoTitle);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setWindowAnimations(R.style.dialog_show_from_down_to_up);
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
        return this;
    }

    public void show() {
        dialog.show();
    }

    public GoListener goListener;
    public interface GoListener{
        void one();
        void two();
        void three();
    }

    public void setGoListener(GoListener goListener) {
        this.goListener = goListener;
    }

    public void dismiss(){
        dialog.dismiss();
    }
}
